package com.uchengwang.ui.cm_ui.login.ui;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.config.SchoolConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.http.manager.HttpManager;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.login.adapter.SelectSchoolAdapter;
import com.uchengwang.ui.cm_ui.ucity.cityBean.SchoolBean;
import com.uchengwang.ui.cm_ui.ucity.cityBean.SchoolListBean;
import com.uchengwang.ui.listener.PermissionsListener;
import com.uchengwang.ui.utils.JsonUtils;
import com.uchengwang.ui.utils.LoadingUtils;
import com.uchengwang.ui.utils.MapManager;
import com.uchengwang.ui.utils.PermissionUtils;
import com.uchengwang.ui.utils.PopupUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import java.util.List;

/**
 * Created by U城网 on 2018/2/7.
 * 注册选择学校
 */

public class SelectSchoolActivity extends BaseWithTitleActivity{

    private LinearLayout add_search;
    private RecyclerView add_rv;
    private SelectSchoolAdapter adapter;
    private EditText search_text;
    private int page = 1, totlaPagers;
    private int type;//1注册时完善信息选择学校后返回 2创建社圈时 没选择学校选择学校
    private View view;
    private TextView popup_search;//搜索显示
    private PopupUtils popupUtils;
    private double lng, lat;//经度, 纬度

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_select_school);
        type = getIntent().getIntExtra(BaseConfig.TYPE, 0);
        setTitle("选择学校");

        initView();

    }

    private void initView(){
        add_search = (LinearLayout) findViewById(R.id.select_search_ln);
        add_search.requestFocus();
        add_search.setFocusableInTouchMode(true);
        add_rv = (RecyclerView) findViewById(R.id.select_rv);

        search_text = (EditText) findViewById(R.id.select_search_text);
        search_text.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (popupUtils != null){
                    popupUtils.dismiss();
                }
                String k = search_text.getText().toString();
                if (TextUtils.isEmpty(k)){
                    return true;
                }
                page = 1;
                getSchools(search_text.getText().toString());
                return true;
            }
        });
        view = LayoutInflater.from(this).inflate(R.layout.popup_view, null, false);

        search_text.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence s, int i, int i1, int i2) {
                if (s.toString() != null && s.length() > 0) {
                    if (view == null){
                        view = LayoutInflater.from(SelectSchoolActivity.this).inflate(R.layout.popup_view, null, false);
                    }
                    popup_search = (TextView) view.findViewById(R.id.popup_search);
                    popup_search.setText(s.toString());
                    popup_search.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            popupUtils.dismiss();
                            page = 1;
                            getSchools(search_text.getText().toString());
                        }
                    });
                    if (popupUtils == null || !popupUtils.isShow()) {
                        popupUtils = PopupUtils.getUtils().showView(SelectSchoolActivity.this, view, false, false, false);
                        popupUtils.showNo(add_search);
                    }
                } else {
                    popupUtils.dismiss();
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        initData();
    }

    private void initData(){
        add_rv.setLayoutManager(new LinearLayoutManager(this));
        adapter = new SelectSchoolAdapter(this, type);
        add_rv.setAdapter(adapter);

        checkLocationPermission();
    }

    /**
     * 检查有没有获取位置的权限
     * */
    private void checkLocationPermission(){
        PermissionUtils.getUtils().checkLocation(this, new PermissionsListener() {
            @Override
            public void onSuccess() {
                getCurrentLocation();
//                getSchools("");
            }

            @Override
            public void onFailed() {
                UCommonUtil.getUtil().showToast(SelectSchoolActivity.this, "请授权获取位置的权限");
            }
        });

    }

    /**
     * 获取当前位置
     * */
    private void getCurrentLocation(){
        MapManager.getMapManager().startLocation(new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation aMapLocation) {
                Log.d("ucw", "定位="+aMapLocation.toString());
                if (aMapLocation != null){
                    lng = aMapLocation.getLongitude();
                    lat = aMapLocation.getLatitude();
                    getSchools("");

                }
            }
        });
    }

    /**
     * 获取学校
     * */
    private void getSchools(String editSing){
        LoadingUtils.getUtils().loading(this);
        MapRequest map = new MapRequest();
        map.put(BaseConfig.PAGE, page);
        map.put(BaseConfig.PERPAGE, 50);

        if (!TextUtils.isEmpty(editSing)){
            map.put(BaseConfig.KEY_WORD, editSing);
        }else {
            if (lng >0){
                map.put(BaseConfig.LNG, lng);
                map.put(BaseConfig.LAT, lat);
            }
        }

        HttpManager.newInstance(this).query(SchoolConfig.GetSchools.method_get, SchoolConfig.GetSchools.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("jjf", "status="+status+"=msg="+msg);
                LoadingUtils.getUtils().hide();
                UCommonUtil.getUtil().hideSoftInput(SelectSchoolActivity.this, getRy());
                UCommonUtil.getUtil().showToast(SelectSchoolActivity.this, "获取失败");
            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("jjf", "status="+status+"=msg="+msg);
                LoadingUtils.getUtils().hide();
                UCommonUtil.getUtil().hideSoftInput(SelectSchoolActivity.this, getRy());

                if (status != 0){
                    UCommonUtil.getUtil().showToast(SelectSchoolActivity.this, msg);
                    return;
                }

                if (!TextUtils.isEmpty(data)){
                    SchoolListBean bean = JsonUtils.getManager().stringToBean(data, SchoolListBean.class);
                    if (bean != null){
                        totlaPagers = bean.getTotalPage();
                        List<SchoolBean> list = bean.getData();
                        if (list != null && list.size() >0){
                            adapter.setData(list);
                        }
                    }
                }
            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();
        MapManager.getMapManager().stopLocation();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MapManager.getMapManager().desLocation();
    }

}
